package com.ncmem.utils;

import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.ReadContext;

import com.ncmem.store.FileBlockWriter;
import com.ncmem.store.StorageType;
import net.sf.json.JSONObject;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;

public class ConfigReader {
    public JSONObject m_files;
    public PathTool m_pt;
    public ReadContext m_jp;

    public ConfigReader()
    {
        this.m_pt = new PathTool();
        try {
            String json = ResTool.readFile("config/config.json");
            this.m_files = JSONObject.fromObject(json);
            this.m_jp = JsonPath.parse(json);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static DataBaseType dbType(){
        ConfigReader r = new ConfigReader();
        String t = r.readString("database.connection.type");

        if(t.compareToIgnoreCase("mysql") ==0) return DataBaseType.MySQL;
        else if(t.compareToIgnoreCase("oracle") ==0) return DataBaseType.Oracle;
        else if(t.compareToIgnoreCase("sql") ==0) return DataBaseType.SqlServer;
        else if(t.compareToIgnoreCase("dmdb") ==0) return DataBaseType.DMDB;
        else if(t.compareToIgnoreCase("kingbase") ==0) return DataBaseType.KingBase;
        else if(t.compareToIgnoreCase("mongodb") ==0) return DataBaseType.MongoDB;
        return DataBaseType.SqlServer;
    }

    /**
     * 将配置加载成一个json对象
     * @param name
     * @return
     */
    public JSONObject module(String name)
    {
        String path = this.m_jp.read(name);
        ClassPathResource res = new ClassPathResource(path);
        String data = null;
        try {
            data = FileTool.readAll(res.getFile());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return JSONObject.fromObject(data);
    }

    public JSONObject read(String name)
    {
        Object o = this.m_jp.read(name);
        return JSONObject.fromObject(o);
    }

    public String readString(String name)
    {
        Object o = this.m_jp.read(name);
        return o.toString();
    }

    /**
     * 以jsonpath方式读取config.json中的bool值
     * @param name
     * @return
     */
    public boolean readBool(String name)
    {
        Object o = this.m_jp.read(name);
        return Boolean.parseBoolean(o.toString());
    }

    /**
     * 获取存储类型
     * @return
     */
    public static StorageType storageType(){
        ConfigReader cr = new ConfigReader();
        String sto = cr.readString("Storage.type");
        if(sto.compareToIgnoreCase("FastDFS") ==0) return StorageType.FastDFS;
        else if(sto.compareToIgnoreCase("Minio") ==0) return StorageType.Minio;
        else if(sto.compareToIgnoreCase("OBS") ==0) return StorageType.OBS;
        else if(sto.compareToIgnoreCase("OSS") ==0) return StorageType.OSS;
        else if(sto.compareToIgnoreCase("COS") ==0) return StorageType.COS;
        return StorageType.IO;
    }

    /**
     * 传输加密
     * 配置：/config.json/security.encrypt
     * @return
     */
    public static boolean postEncrypt() {
        ConfigReader cr = new ConfigReader();
        return cr.readBool("security.encrypt");
    }

    public static boolean securityToken() {
        ConfigReader cr = new ConfigReader();
        return cr.readBool("security.token");
    }

    /**
     * 存储加密
     * @return
     */
    public static boolean storageEncrypt(){
        ConfigReader cr = new ConfigReader();
        return cr.readBool("Storage.encrypt");
    }

    /**
     * 块数据写入器
     * @return
     */
    public static FileBlockWriter blockWriter(){
        FileBlockWriter w = null;
        StorageType st = ConfigReader.storageType();
        //if(st==StorageType.FastDFS) w = new FastDFSWriter();
        //else if(st==StorageType.Minio) w = new MinioWriter();
        //else if(st==StorageType.OBS) w = new ObsWriter();
        //else if(st==StorageType.OSS) w = new OSSWriter();
        //else if(st==StorageType.COS) w = new CosWriter();
        w = new FileBlockWriter();
        return w;
    }
}